package ${package}.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.exception.BadRequestException;
import ${package}.domain.${className};
import ${package}.service.${className}Service;
import ${package}.service.dto.${className}DTO;
import ${package}.service.query.${className}QueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import me.zhengjie.modules.system.service.dto.DictDetailDTO;
import me.zhengjie.modules.system.service.query.DictDetailQueryService;
import me.zhengjie.utils.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.sql.Timestamp;
import java.util.Date;
/**
* @author ${author}
* @date ${date}
*/
@RestController
@RequestMapping("api")
public class ${className}Controller {

    @Autowired
    private ${className}Service ${changeClassName}Service;

    @Autowired
    private ${className}QueryService ${changeClassName}QueryService;

    @Autowired
    private DictDetailQueryService dictDetailQueryService;

    private static final String ENTITY_NAME = "${changeClassName}";

    @Log("查询${className}")
    @GetMapping(value = "/${changeClassName}")
    public ResponseEntity get${className}s(${className}DTO resources, Pageable pageable){
        return new ResponseEntity(${changeClassName}QueryService.queryAll(resources,pageable),HttpStatus.OK);
    }

    @Log("新增${className}")
    @PostMapping(value = "/${changeClassName}")
    public ResponseEntity create(@Validated @RequestBody ${className} resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        UserDetails userDetails = SecurityContextHolder.getUserDetails();
        resources.setCreator(userDetails.getUsername());
        resources.setUpdator(userDetails.getUsername());
        Date now = new Date();
        resources.setCreateTime(new Timestamp(now.getTime()));
        resources.setUpdateTime(new Timestamp(now.getTime()));
        return new ResponseEntity(${changeClassName}Service.create(resources),HttpStatus.CREATED);
    }

    @Log("修改${className}")
    @PutMapping(value = "/${changeClassName}")
    public ResponseEntity update(@Validated @RequestBody ${className} resources){
        if (resources.getId() == null) {
            throw new BadRequestException(ENTITY_NAME +" ID Can not be empty");
        }
        UserDetails userDetails = SecurityContextHolder.getUserDetails();
        resources.setUpdator(userDetails.getUsername());
        Date now = new Date();
        resources.setUpdateTime(new Timestamp(now.getTime()));
        ${changeClassName}Service.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除${className}")
    @DeleteMapping(value = "/${changeClassName}/{id}")
    public ResponseEntity delete(@PathVariable Long id){
        ${changeClassName}Service.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log("获取AccessDevice所需字典")
    @GetMapping(value = "/${changeClassName}/dict")
    public ResponseEntity getDict() {
        Map<String, Object> map = new HashMap<>();
        DictDetailDTO isNoDto = new DictDetailDTO();
        isNoDto.setDictName("is_no");
        map.put("is_no", dictDetailQueryService.queryAll(isNoDto));
        return new ResponseEntity(map, HttpStatus.OK);
    }
}